#include "unp.h"

int main(int argc, char * argv[]) {
	if (argc < 2) {
		printf("usage %s <ip address>", argv[0]);
		exit(1);
	}


	SA_IN serv_addr;
	int sockfd;

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);
	bzero((char*)&serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &(serv_addr.sin_addr));
	Connect(sockfd, (SA*)&serv_addr, sizeof(serv_addr));
	str_cli(stdin, sockfd);
	return 0;
}

void str_cli(FILE* fp, int sockfd) {
	char sendline[MAXLINE], recvline[MAXLINE];
	ssize_t n;
	bzero(sendline, MAXLINE);
	bzero(recvline, MAXLINE);

	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		n = Write(sockfd, sendline, strlen(sendline));
		n = Read(sockfd, recvline, MAXLINE);
		Fputs(recvline, stdout);
	}
}